﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.Web.Controllers.Order.AcceptModel;
using Core.Web.Controllers.Project.AcceptModel;
using Core.Web.Model;
using Microsoft.AspNetCore.Mvc;

namespace Core.Web.Controllers.Project
{
    /// <summary>
    /// 项目服务
    /// </summary>
    [Route(".v1/project/[controller]")]
    [ApiController]
    [ApiProjectMemberAuthorize]
    public class ApiServiceController : BaseApiController
    {
        /// <summary>
        /// 获取服务列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [IgnoreAuthorize]
        public IResult GetServiceList()
        {
            var result = this.iProjectUserApiService.GetProjectApiList(iProjectInfo.Key);

            return new ApiResult
            {
                code = result.IsFinished ?
                        CodeResult.SUCCESS :
                        CodeResult.BUSINESS_ERROR,
                info = result.IsFinished ? null : result.Discription,
                date = result.Date
            };
        }

        /// <summary>
        /// 订单发起付款
        /// [支付宝]
        /// </summary>
        /// <returns></returns>
        [HttpPost(".aliApplyPay")]
        public IResult AliApplyPay(PayRequest request)
        {
            if (iProjectInfo.PayAliInfo == null)
            {
                return new ApiResult
                {
                    code = CodeResult.CONFIG_ERROR
                };
            }

            var result = this.iPay.AliServiceOrderPay(request.title, request.orderKey, iProjectInfo);

            if (result.IsFinished)
            {
                return new ApiResult
                {
                    code = CodeResult.SUCCESS,
                    date = new
                    {
                        price = result.Date.Key,
                        orderInfo = result.Date.Value
                    }
                };
            }

            return new ApiResult
            {
                code = CodeResult.DATA_NOT_FOUND,
                info = result.Discription
            };

        }

        /// <summary>
        /// 订单发起付款
        /// [微信]
        /// </summary>
        /// <returns></returns>
        [HttpPost(".wChatApplyPay")]
        public IResult WChatApplyPay(PayRequest request)
        {
            if (iProjectInfo.PayWChatInfo == null)
            {
                return new ApiResult
                {
                    code = CodeResult.CONFIG_ERROR
                };
            }

            var result = this.iPay.WChatServiceOrderPay(request.title, request.orderKey, iProjectInfo);

            if (result.IsFinished)
            {
                return new ApiResult
                {
                    code = CodeResult.SUCCESS,
                    date = new
                    {
                        price = result.Date.Key,
                        orderInfo = result.Date.Value
                    }
                };
            }

            return new ApiResult
            {
                code = CodeResult.DATA_NOT_FOUND,
                info = result.Discription
            };

        }



        /// <summary>
        /// 订单已付款
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns></returns>
        [HttpPost(".payment")]
        public IResult OrderPayment(AliApplyPayRequest request)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iProjectUserApiService.UserOrderPay(request.orderKey));
                  });
        }


    }
}